*GENERAL INFO
	* Project: How to Close the Gender Gap in Political Participation: Lessons from Matrilineal Societies in Africa
	* Authors: Amanda Robinson and Jessica Gottlieb
	* Date created: Dec. 2018
* DO FILE INFO
	* This .do file performs all reported analyses on the cross-national data (RG_Matlin_XNat.dta).

* SET WORKING DIRECTORY
	* Set the working directory to the folder in which all replication files are stored.
		
****************************************************************
clear

* Produce a log file
*log using RG_Matlin_XNat_log, replace

set more off
use RG_Matlin_XNat.dta

*** Create Macros for DVs ***

global dv engindexA polindexA civindexA 
global dv_eng z_polinterest z_poldiscuss z_polunderstand
global dv_politic z_voted z_contactpol z_protest z_rally z_persuade z_campaign  
global dv_civic z_meeting z_commorg z_leader z_colaction
global countries country1-country25		


*******************************
***** Main Regression DiD *****
*******************************

set more off
foreach d in  dv  dv_eng dv_politic dv_civic   { 

foreach y in $`d' {
local vtext : variable label `y' 
xtmixed `y' i.female##i.matlin  i.country || egroup: 
estimates store `y'
local c : di %4.3f _b[1.female#1.matlin]
matrix table=r(table)
local p= table[4,8]
local p: di %4.3f `p'

* predicted probabilities
margins, by( matlin female) atmeans vsquish
marginsplot, plotdimension(female) ///
	title (`"`vtext'"', color(black) size(medium)) ytitle("") xtitle("") ///estt
	plot1opts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	plot2opts(msymbol(S) mcolor(gs10) lcolor(gs10) ) ci2opts(lcolor(gs10) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white))  ///
	xscale(range(-0.5 1.5)) xlabel(0 1, labs(small)) xsize(1) ysize(1.5) ///
	legend (order (1 "Men" 2 "Women") region(lcolor(white)) c(2)) ///
	note("Diff-in-Diff: `c', p=`p'", ring(0) position(2)) /// //
	name(`y'_pp, replace) nodraw 

* marginal effects
margins, dydx(female) by(matlin) atmeans vsquish
marginsplot,  ///
	title (`"`vtext'"', color(black) size(medium)) ytitle("") xtitle("") ///estt
	plotopts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white))  ///
	xscale(range(-0.5 1.5)) xlabel(0 1, labs(small)) xsize(1) ysize(1.5) ///
	legend(off)   ///
	note("Diff-in-Diff: `c', p=`p'", ring(0) position(2)) /// 
	name(`y', replace) nodraw
}	
 
* Figure 2
graph combine $`d', graphregion(fcolor(white) ilcolor(white) lcolor(white)) xcommon ycommon ///
	l1title("Marginal Effect of Female", size(medium)) ///
	cols(1) name(graph, replace)
graph display graph, xsize(5) ysize(10)
*graph export "Figures/`d'_matlin_me.pdf", replace
}

* Figure A.1
grc1leg engindexA_pp polindexA_pp civindexA_pp, graphregion(fcolor(white) ilcolor(white) lcolor(white)) xcommon ycommon  cols(1) name(graph, replace) ///
	l1title("Predicted Values", size(msmall))
graph display graph, xsize(5) ysize(10)
*graph export "Figures/dv_matlin_pp.pdf", replace

*******************************
* Robustness to using country random effects

set more off
foreach d in  dv     { 

foreach y in $`d' {
local vtext : variable label `y' 
xtmixed `y' i.female##i.matlin   || country: || egroup: 
estimates store `y'_re
local c : di %4.3f _b[1.female#1.matlin]
matrix table=r(table)
local p= table[4,8]
local p: di %4.3f `p'

margins, dydx(female) by(matlin) atmeans vsquish
marginsplot,  ///
	title (`"`vtext'"', color(black) size(medium)) ytitle("") xtitle("") ///estt
	plotopts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white))  ///
	xscale(range(-0.5 1.5)) xlabel(0 1, labs(small)) xsize(1) ysize(1.5) ///
	legend(off) yscale(range(0)) ///
	note("Diff-in-Diff: `c', p=`p'", ring(0) position(2)) /// 
	name(`y', replace) nodraw
}	
 
*Figure A.3
graph combine $`d', graphregion(fcolor(white) ilcolor(white) lcolor(white)) xcommon ycommon cols(1) name(graph, replace) ///
	l1title("Marginal Effect of Female", size(medium))
graph display graph, xsize(5) ysize(10)
*graph export "Figures/`d'_matlin_re.pdf", replace
}


*******************************
* Robustness to individual-level controls (age, wealth, urban, formal sector occupation)

global con age wealth edu urban employed

set more off
foreach d in  dv    { 

foreach y in $`d' {
local vtext : variable label `y' 
xtmixed `y' i.female##i.matlin i.female##c.age i.female##c.wealth i.female##c.edu i.female##i.urban i.female##i.employed i.country || egroup: 
estimates store `y'_con
local c : di %4.3f _b[1.female#1.matlin]
matrix table=r(table)
local p= table[4,8]
local p: di %4.3f `p'

* marginal effects
margins, dydx(female) by(matlin) atmeans vsquish
marginsplot,  ///
	title (`"`vtext'"', color(black) size(medium)) ytitle("") xtitle("") ///estt
	plotopts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white))  ///
	xscale(range(-0.5 1.5)) xlabel(0 1, labs(small)) xsize(1) ysize(1.5) ///
	legend(off)   ///
	note("Diff-in-Diff: `c', p=`p'", ring(0) position(2)) /// 
	name(`y', replace) nodraw
}	
*Figure A.2
graph combine $`d', graphregion(fcolor(white) ilcolor(white) lcolor(white)) xcommon ycommon ///
	l1title("Marginal Effect of Female", size(medium)) ///
	cols(1) name(graph, replace)
graph display graph, xsize(5) ysize(10)
*graph export "Figures/`d'_matlin_controls.pdf", replace
}



*******************************
* Robustness to controlling for cattle dependence and the plow

set more off
foreach d in  dv     { 

foreach y in $`d' {
local vtext : variable label `y' 
xtmixed `y' i.female##i.matlin i.female##i.cattle i.female##i.plow  i.country || egroup: 
estimates store `y'_cattle
local c : di %4.3f _b[1.female#1.matlin]
matrix table=r(table)
local p= table[4,8]
local p: di %4.3f `p'

margins, dydx(female) by(matlin) atmeans vsquish
marginsplot,  ///
	title (`"`vtext'"', color(black) size(medium)) ytitle("") xtitle("") ///estt
	plotopts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white))  ///
	xscale(range(-0.5 1.5)) xlabel(0 1, labs(small)) xsize(1) ysize(1.5) ///
	legend(off) yscale(range(0)) ///
	note("Diff-in-Diff: `c', p=`p'", ring(0) position(2)) ///
    note("Controlling for Cattle Dependence and Plow Use", ring(0) position(5)) ///
	name(`y', replace) nodraw

}	
 
*Figure A.9
graph combine $`d', graphregion(fcolor(white) ilcolor(white) lcolor(white)) xcommon ycommon cols(1) name(graph, replace) ///
		l1title("Marginal Effect of Female", size(medium))
graph display graph, xsize(5) ysize(10)
*graph export "Figures/`d'_matlin_cattle.pdf", replace
}




*********************************
* Cattle Dependence/Plow Use and Matrilineality (Ethnic Group Level)
*******************************

tab cattle if etag==1, summ(matlin)
tab cattle if etag==1, summ(plow)
ttest matlin if etag==1, by(plow)

*Figure A.7
reg matlin i.cattle i.country if etag==1 
est sto cattle
margins, predict() by(cattle)
marginsplot,  ///
	title ("Cattle Dependence and Matrilineal Kinship Across Ethnic Groups", color(black) size(medium)) /// 
    ytitle("Predicted Liklihood of Matrilineal Kinship") xtitle("Historical Dependence on Cattle") ///estt
	plot1opts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white)) 
*graph export "Figures/cattle_matlin.pdf", replace
 
*Figure A.8
reg matlin plow i.country if etag==1
est sto plow
margins, predict() by(plow)
marginsplot,  ///
	title ("Plow Use and Matrilineal Kinship Across Ethnic Groups", color(black) size(medium)) /// 
    ytitle("Predicted Liklihood of Matrilineal Kinship") xtitle("Historical Use of the Plow") ///estt
	plot1opts(msymbol(O) mcolor(black) lcolor(black) ) ci1opts(lcolor(black) msiz(0))   ///
	graphregion(fcolor(white) ilcolor(white) lcolor(white)) 
*graph export "Figures/plow_matlin.pdf", replace

reg matlin i.cattle plow i.country if etag==1
est sto cattle_plow 


*******************************
*** Statistics reported in text ***
*******************************

distinct country if respno~="" 	// # of AB countries
distinct respno if respno~="" 	// # of AB respondents
distinct cegroup if respno~="" 	// # of AB country-ethnic groups
distinct egroup if respno~="" 	// # of AB ethnic groups (can cross borders)
distinct murdock			 	// # of EA ethnic groups in SSA

distinct country if respno~="" & varmatlin==1 // # of AB countries w/ matlin variation
distinct respno if respno~="" & varmatlin==1	// # of AB respondents w/ matlin variation
distinct cegroup if respno~="" & varmatlin==1	// # of AB country-ethnic groups w/ matlin variation

distinct egroup if _mergeEA==3 & respno~=""	// # of AB ethnic groups matched to EA
distinct respno if _mergeEA==3 & respno~=""	// # of AB respondnets matched to EA
distinct respno if respno~="" & egroup~=.	// # of AB respondents gave ethnicity
distinct respno if respno~="" & matlin~=.	// # of AB respondents with descent rule info
distinct egroup if respno~="" & matlin~=.	// # of AB ethnic groups with descent rule info
distinct country if respno~="" & matlin~=.	// # of AB countries with descent rule info

tab matlin if etag==1		// prop. of matrilineal ethnic groups 
tab matlin if respno~=""	// prop. of matrilineal respondents

tab country if matlin==1 & respno~="" // matrilineal countries
tab country, summ(matlin)				// prop. of countries that are matlin

bys matlin: tab matloc if etag==1	// prop. of matlin groups that are matlocal

egen mtag=tag(murdock)
tab matlin if mtag==1	// prop. of EA groups that are matrilineal



*******************************
*** Tables ****
*******************************

* Add number of group-country observations 
global est $dv $dv_eng $dv_politic $dv_civic engindexA_cattle polindexA_cattle civindexA_cattle
foreach e in $est {
est restore `e'
mat N_g =e(N_g)
estadd scalar Nc=N_g[1,1]: `e'
estadd scalar Nr=N_g[1,2]: `e'
}


*Produce regression tables

    * Index DVs
* Table A.2
esttab $dv  /// 
	, star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
    stats(Nc N,layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels("Ethnic Groups" "Individuals") f(0)) ///
	title(Matrilineality and the Gender Gap in Civic and Political Engagement and Participation\label{tab:dv})  ///
	drop( *country) ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant) ///
	nonotes  nobaselevels  nogaps ///
    sub("\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}") ///
	addnote("Multi-level models in which individuals are nested within country-specific ethnic groups with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")
*Table A.5
esttab $dv_eng  /// 
	, star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
    stats(Nc N,layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels("Ethnic Groups" "Individuals") f(0)) ///
	title(Matrilineality and the Gender Gap in Political Engagement Outcomes\label{tab:dv_eng})  ///
	drop( *country) ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant) ///
	nonotes  nobaselevels  nogaps ///
    sub("\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}") ///
	addnote("Multi-level models in which individuals are nested within country-specific ethnic groups with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")
*Table A.6
esttab $dv_civic  /// 
	, star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
    stats(Nc N,layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels("Ethnic Groups" "Individuals") f(0)) ///
	title(Matrilineality and the Gender Gap in Civic Participation Outcomes\label{tab:dv_civic})  ///
	drop( *country) ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant) ///
	nonotes  nobaselevels  nogaps ///
    substitute({table} {sidewaystable} \_ _ "\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}") ///	
    addnote("Multi-level models in which individuals are nested within country-specific ethnic groups with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")
*Table A.7
esttab $dv_politic  /// 
	, star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
    stats(Nc N,layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels("Ethnic Groups" "Individuals") f(0)) ///
	title(Matrilineality and the Gender Gap in Political Participation Outcomes\label{tab:dv_politic})  ///
	drop( *country) ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant) ///
	nonotes  nobaselevels  nogaps ///
	substitute({table} {sidewaystable} \_ _ "\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}") ///
	addnote("Multi-level models in which individuals are nested within country-specific ethnic groups with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")

* Including Individual-Level Controls
*Table A.3
esttab engindexA_con polindexA_con civindexA_con   /// 
	, star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
	stats(N,layout("\multicolumn{1}{c}{@}") labels("Observations (Individual)") f(0))  ///
	title(Matrilineality and the Gender Gap in Civic and Political Outcome, Controlling for Demographics \label{tab:dv_controls} )  ///
	drop( *country) ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant ///
	1.urban Urban 1.female#1.urban  "Female $\times$ Urban" 1.employed Employed 1.female#1.employed  "Female $\times$ Employed" ///
    age "Age" 1.female#c.age  "Female $\times$ Age" wealth "Wealth Index" 1.female#c.wealth  "Female $\times$ Wealth" ///
    edu "Education Level" 1.female#c.edu  "Female $\times$ Education") ///
	nonotes  nobaselevels nogaps ///
    refcat(1.cattle "\textit{Cattle Dependence}", nolabel) ///
	sub("\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}" % \%) ///
	addnote("Multi-level models in which individuals are nested within country-specific ethnic groups with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")

	
* Country Random Effects	
*Table A.4
esttab engindexA_re polindexA_re civindexA_re   /// 
	, star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
	stats(N,layout("\multicolumn{1}{c}{@}") labels("Observations (Individual)") f(0))  ///
	title(Matrilineality and the Gender Gap in Civic and Political Outcome, with Country Random Effects \label{tab:dv_re} )  ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Country)" "sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant) ///
	nonotes  nobaselevels nogaps ///
    refcat(1.cattle "\textit{Cattle Dependence}", nolabel) ///
	sub("\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}" % \%) ///
	addnote("Multi-level models in which individuals are nested within country-specific ethnic groups and countries." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")


* Controlling for Cattle/Plow
*Table A.8
esttab engindexA_cattle polindexA_cattle civindexA_cattle   /// 
	, star(* 0.10 ** 0.05 *** 0.01)  b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
	stats(N,layout("\multicolumn{1}{c}{@}") labels("Observations (Individual)") f(0))  ///
	title(Matrilineality and the Gender Gap in Civic and Political Outcome, Controlling for Cattle Dependence and Plow Use \label{tab:dv_cattle} )  ///
	drop( *country) ///
	transform(ln*: exp(@) exp(@)) ///
	eqlabels("" "\midrule sd(Ethnic Group)" "sd(Residual)", none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant /// 
    1.plow "Plow Use" 1.female#1.plow "Female $\times$ Plow Use") ///
	nonotes  nobaselevels nogaps ///
    refcat(1.cattle "\textit{Cattle Dependence}", nolabel) ///
	sub("\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}" % \%) ///
	addnote("Multi-level models in which individuals are nested within country-specific ethnic groups with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")


* Correlates of Matrilineality (Ethnic Group Level)
*Table A.9
esttab cattle plow cattle_plow   /// 
	, star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label booktabs alignment(D{.}{.}{-1}) replace ///
	stats(N,layout("\multicolumn{1}{c}{@}") labels("Ethnic Groups") f(0))  ///
	title(Relationship Between Cattle Dependence/Plow Use and Matrilineal Kinship \label{tab:cattle} ) ///
	drop(*country) eqlab(, none) ///
	varlabels (1.female Female 1.female#1.matlin "Female $\times$ Matrilineal" 1.matlin Matrilineal _cons Constant) ///
	nonotes  nobaselevels  nogaps ///
    refcat(1.cattle "\textit{Cattle Dependence}", nolabel) ///
	sub("\begin{tabular}" "\scalebox{0.7}{\begin{tabular}" "\end{tabular}" "\end{tabular}}" % \%) ///
	addnote("Linear model with country fixed effects." "$^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")







***** Summary Statistics *****

global dv_vars matlin $dv $dv_eng $dv_politic $dv_civic 


*latex tables
foreach v of varlist $dv_eng $dv_politic $dv_civic   {
	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'
	}

set more off
estpost su $dv_vars if female==0
est store A
estpost su $dv_vars if female==1
est store B
estpost su $dv_vars
est store C

* Table A.1
esttab A B C , replace booktabs ///
		mtitle("\textbf{Men}" "\textbf{Women}" "\textbf{Overall}") ///
		cells(mean(fmt(2))) label  nonum  collabels(none) gaps  noobs ///
		order(matlin engindexA $dv_eng polindexA $dv_politic civindexA $dv_civic ) ///
   		refcat(matlin "\textbf{Descent Type}" engindexA "\textbf{Political Engagement}" polindexA "\textbf{Political Participation}" civindexA "\textbf{Civic Participation}", nolabel)
		

* Close log and convert log file
*log close
*translate RG_Matlin_XNat_log.smcl RG_Matlin_XNat_log.pdf, replace
